$(function () {
    /*
     * 抽取所有需要用得元素.
     */
    var followupBillDatagrid, followupBillDialog, followupBillForm,followupBillSearchBtn;
    followupBillDatagrid = $("#followupBill_datagrid");
    followupBillDialog = $("#followupBill_dialog");
    followupBillForm = $("#followupBill_form");
    followupBillSearchBtn = $("#searchBtn");
    /*
     * 初始化数据表格
     */
    followupBillDatagrid.datagrid({
        url: "/followupBill/list",
        fit: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pagination: true,
        fitColumns: true,
        toolbar: '#followupBill_datagrid_tb',
        columns: [
            [
                {field: 'content', width: 10, align: 'center', title: '跟进内容'},
                {field: 'followtime', width: 10, align: 'center', title: '跟进时间'},
                {field: 'conclusion', width: 10, align: 'center', title: '跟进总结'},
                {field: 'feedback', width: 10, align: 'center', title: '客户反馈'},
                {field: 'client', width: 10, align: 'center', title: '跟进客户',formatter: function (value, row, index) {
                    return value ? value.realname :"未知客户";
                }}
            ]
        ]
    });
    followupBillSearchBtn.textbox({
        width:230,
        label:"关键字:",
        labelWidth:50,
        prompt:"请输入搜索关键字",
        buttonText:'搜索',
        buttonIcon:'icon-search',
        onClickButton:function(){
            var keyword = $(this).val();
            followupBillDatagrid.datagrid("load",{
                keyword:keyword
            });
        }
    });
    /*
     * 初始化新增/编辑对话框
     */
    followupBillDialog.dialog({
        width: 300,
        height: 250,
        closed: true,
        buttons: '#followupBill_dialog_bt'
    });
    /*
     * 对页面按钮进行统一监听
     */
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    /*
     * 所有的操作封装到cmdObj对象中,方便管理
     */
    var cmdObj = {
        //增删改,
        add: function () {
            //把缓存中所有的数据重新加载allPermission的数据表格中
            followupBillForm.form("clear");
            followupBillDialog.dialog("setTitle", "新增");
            followupBillDialog.dialog("open");
        },
        edit: function () {
            var rowData = followupBillDatagrid.datagrid("getSelected");
            if (rowData) {
                followupBillForm.form("clear");
                followupBillDialog.dialog("setTitle", "编辑");
                followupBillDialog.dialog("open");
                followupBillForm.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择需要编辑的数据!", "warining");
            }
        },
        del: function () {
            var rowData = followupBillDatagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "您确定需要删除选中数据吗？", function (yes) {
                    if (yes) {
                        $.get("/followupBill/delete?followupBillId=" + rowData.id, function (data) {
                            if (data.success) {
                                $.messager.alert("温馨提示", data.msg, "info", function () {
                                    followupBillDatagrid.datagrid("reload");
                                });
                            } else {
                                $.messager.alert("温馨提示", data.msg, "error");
                            }
                        }, "json")
                    }
                });
            } else {
                $.messager.alert("温馨提示", "请选择需要删除的数据!", "warining");
            }
        },
        //刷新,确认,取消
        reload: function () {
            followupBillDatagrid.datagrid("reload");
        },
        save: function () {
            var url;
            var idVal = $("[name='id']").val();
            if (idVal) {
                url = "/followupBill/update"
            } else {
                url = "/followupBill/save";
            }
            followupBillForm.form("submit", {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            followupBillDialog.dialog("close");
                            followupBillDatagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "error");
                    }
                }
            });
        },
        cancel: function () {
            followupBillDialog.dialog("close");
        }
    }
});
